/*
 * @author : TF-BJ-C064
 * @creation : 2014-6-19 下午6:13:59
 * @description : 
 *
 */

package com.action.system;

import java.util.List;

import com.base.BaseAction;
import com.bean.RoleInfo;
import com.bean.SysUser;
import com.service.RoleService;
import com.service.SysUserService;
import com.util.GUtil;

public class SysUserAction extends BaseAction {

	RoleService roleService;
	List<RoleInfo> listRole;
	SysUserService sysUserService;
	SysUser userInfo;
	List<SysUser> list;
	Integer flag;

	@Override
	public String handle() throws Exception {
		if(id!=null && id>0)
			return this.view();
		return this.query();
	}

	@Override
	public String query() throws Exception {
		list = sysUserService.query(userInfo);
		return GUtil.LIST;
	}

	@Override
	public String add() throws Exception {
		this.setFlag(0);
		this.setHeaderTitle("添加用户");
		listRole = roleService.query(null);
		if(userInfo==null){
			return GUtil.EDIT;
		}
		if(sysUserService.add(userInfo).equals(GUtil.SUCCESS)){
			this.setMsg("添加成功");
			userInfo = null;
		}else{
			this.setMsg("添加失败");
		}
		return GUtil.EDIT;
	}

	@Override
	public String update() throws Exception {
		this.setHeaderTitle("修改用户");
		this.setFlag(1);
		if(userInfo==null){
			if(id==null || id<1)
				return GUtil.ERROR;
			userInfo = sysUserService.get(Long.valueOf(id));
			listRole = roleService.query(null);
			session.put("userInfo", userInfo);
			return GUtil.EDIT;
		}

		SysUser su = (SysUser) session.get("userInfo");
		if(su==null){
			this.setMsg("修改超时");
		}else{
			userInfo.setSysUserId(su.getSysUserId());//SET ID
			userInfo.setUserName(su.getUserName());//SET USERNAME
			if(sysUserService.update(userInfo).equals(GUtil.SUCCESS)){
				this.setMsg("修改成功");
				userInfo = null;
			}else{
				this.setMsg("修改失败");
			}
		}
		return this.query();
	}

	@Override
	public String delete() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		if(sysUserService.delete(Long.valueOf(id)).equals(GUtil.SUCCESS)){
			this.setMsg("删除成功");
			userInfo = null;
			return this.query();
		}else{
			this.setMsg("删除失败");
			return GUtil.LIST;
		}
	}

	@Override
	public String view() throws Exception {
		return GUtil.VIEW;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

	/**
	 * @return the sysUserService
	 */
	public SysUserService getSysUserService() {
		return sysUserService;
	}

	/**
	 * @param sysUserService the sysUserService to set
	 */
	public void setSysUserService(SysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}

	/**
	 * @return the list
	 */
	public List<SysUser> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<SysUser> list) {
		this.list = list;
	}

	/**
	 * @return the roleService
	 */
	public RoleService getRoleService() {
		return roleService;
	}

	/**
	 * @param roleService the roleService to set
	 */
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @return the listRole
	 */
	public List<RoleInfo> getListRole() {
		return listRole;
	}

	/**
	 * @param listRole the listRole to set
	 */
	public void setListRole(List<RoleInfo> listRole) {
		this.listRole = listRole;
	}

	/**
	 * @return the flag
	 */
	public Integer getFlag() {
		return flag;
	}

	/**
	 * @param flag the flag to set
	 */
	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	/**
	 * @return the userInfo
	 */
	public SysUser getUserInfo() {
		return userInfo;
	}

	/**
	 * @param userInfo the userInfo to set
	 */
	public void setUserInfo(SysUser userInfo) {
		this.userInfo = userInfo;
	}

}
